package com.inspur.xiaoju.security;

import com.inspur.xiaoju.model.SurveyResponse;
import com.inspur.xiaoju.util.EncryptionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class ResponseSecurityPlugin implements SecurityPlugin {
    
    @Value("${xiaoju.security.secret-key:defaultSecretKey}")
    private String secretKey;
    
    @Override
    public void encryptResponseData(SurveyResponse responseData) {
        List<String> secretKeys = new ArrayList<>();
        if (responseData.getData() != null) {
            for (Map.Entry<String, Object> entry : responseData.getData().entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                boolean needEncrypt = false;
                
                if (value instanceof List) {
                    List<?> values = (List<?>) value;
                    for (Object val : values) {
                        if (isDataSensitive(val)) {
                            needEncrypt = true;
                            break;
                        }
                    }
                } else {
                    if (isDataSensitive(value)) {
                        needEncrypt = true;
                    }
                }
                
                if (needEncrypt) {
                    secretKeys.add(key);
                    if (value instanceof List) {
                        List<?> values = (List<?>) value;
                        List<Object> encryptedValues = new ArrayList<>();
                        for (Object val : values) {
                            encryptedValues.add(EncryptionUtil.encryptData(val, secretKey));
                        }
                        responseData.getData().put(key, encryptedValues);
                    } else {
                        responseData.getData().put(key, EncryptionUtil.encryptData(value, secretKey));
                    }
                }
            }
        }
        responseData.setSecretKeys(secretKeys);
    }
    
    @Override
    public void decryptResponseData(SurveyResponse responseData) {
        List<String> secretKeys = responseData.getSecretKeys();
        if (secretKeys != null && !secretKeys.isEmpty()) {
            for (String key : secretKeys) {
                Object value = responseData.getData().get(key);
                if (value instanceof List) {
                    List<?> values = (List<?>) value;
                    List<Object> decryptedValues = new ArrayList<>();
                    for (Object val : values) {
                        decryptedValues.add(EncryptionUtil.decryptData(val, secretKey));
                    }
                    responseData.getData().put(key, decryptedValues);
                } else {
                    responseData.getData().put(key, EncryptionUtil.decryptData(value, secretKey));
                }
            }
        }
        responseData.setSecretKeys(new ArrayList<>());
    }
    
    private boolean isDataSensitive(Object data) {
        // 实现敏感数据检测逻辑
        return data != null && data.toString().matches(".*\\d{11}.*|.*@.*\\..*|.*\\d{18}.*");
    }
}